<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';

if($_RQ['op']=='mian'){
	set_time_limit(0);
		$ret = array();
		$url = $_RQ['url'];
		$cates = $_RQ['cate'];
		
		$url='https://item.jd.com/100000287113.html';

		if (is_numeric($url)) {
			$itemid = $url;
		}
		else {
			preg_match('/(\\d+).html/i', $url, $matches);

			if (isset($matches[1])) {
				$itemid = $matches[1];
			}
		}

		if (empty($itemid)) {
			exi('未获取到 itemid!','error');
		}
		$ret = get_item_jingdong($itemid, $url, $cates);
		//plog('jingdong.main', '京东抓取宝贝 京东id:' . $itemid);
		exit(json_encode($ret));
}

function get_item_jingdong($itemid = '', $jingdongurl = '', $cates = '', $merchid = 0) 
	{
		error_reporting(0);
		$g = pdo_fetch('select * from ' . tablename('jy_weishop_goods') . ' where uniacid=:uniacid and merchid=:merchid and catch_id=:catch_id and catch_source=\'jingdong\' limit 1', array(':uniacid' => $_SESSION['uniacid'], ':catch_id' => $itemid, ':merchid' => $merchid));
		$url = get_jingdong_info_url($itemid);
		
		//load()->func('communication');
		$response = cfc('http')->ihttp_get($url);
		$length = strval($response['headers']['Content-Length']);
		
		if ($length != NULL) 
		{
			exi('未从京东获取到商品信息!','error');
		}
		$content = $response['content'];
		
		if (strpos($content, 'location.href="/unifiedlogin/checkcredentials.action?returnUrl=http://mitem.jd.hk/ware/view.action?wareId=')) 
		{  
			get_item_jdHK($itemid, $jingdongurl, $cates, $merchid);
			return;
		}
		
		$dom = new DOMDocument();
		$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>' . $content);
		$xml = simplexml_import_dom($dom);
		$prodectNameContent = $xml->xpath('//*[@class="sku-name"]');
		//file_put_contents('jd.txt',$dom);
		print_r($prodectNameContent);exit;
		$prodectName = strval( $prodectNameContent[0]->attributes()->value);
		
		if ($prodectName == NULL) 
		{
			exi('宝贝不存在!','error');
		}
		$item = array();
		$item['id'] = $g['id'];
		$item['merchid'] = $merchid;
		
		if (!(empty($merchid))) 
		{
			if (empty($_W['merch_user']['goodschecked'])) 
			{
				$item['checked'] = 1;
			}
			else 
			{
				$item['checked'] = 0;
			}
		}
		$pcates = array();
		$ccates = array();
		$tcates = array();
		$pcateid = 0;
		$ccateid = 0;
		$tcateid = 0;
		/*
		if (is_array($cates)) 
		{
			foreach ($cates as $key => $cid ) 
			{
				$c = pdo_fetch('select level from ' . tablename('jy_weishop_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $cid, ':uniacid' => $_SESSION['uniacid']));
				if ($c['level'] == 1) 
				{
					$pcates[] = $cid;
				}
				else if ($c['level'] == 2) 
				{
					$ccates[] = $cid;
				}
				else if ($c['level'] == 3) 
				{
					$tcates[] = $cid;
				}
				if ($key == 0) 
				{
					if ($c['level'] == 1) 
					{
						$pcateid = $cid;
					}
					else if ($c['level'] == 2) 
					{
						$crow = pdo_fetch('select parentid from ' . tablename('jy_weishop_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $cid, ':uniacid' => $_SESSION['uniacid']));
						$pcateid = $crow['parentid'];
						$ccateid = $cid;
					}
					else if ($c['level'] == 3) 
					{
						$tcateid = $cid;
						$tcate = pdo_fetch('select id,parentid from ' . tablename('jy_weishop_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $cid, ':uniacid' => $_SESSION['uniacid']));
						$ccateid = $tcate['parentid'];
						$ccate = pdo_fetch('select id,parentid from ' . tablename('jy_weishop_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $ccateid, ':uniacid' => $_SESSION['uniacid']));
						$pcateid = $ccate['parentid'];
					}
				}
			}
		}
		*/
		
		$item['pcate'] = $pcateid;
		$item['ccate'] = $ccateid;
		$item['tcate'] = $tcateid;
		if (!(empty($cates))) 
		{
			$item['cates'] = implode(',', $cates);
		}
		$item['pcates'] = implode(',', $pcates);
		$item['ccates'] = implode(',', $ccates);
		$item['tcates'] = implode(',', $tcates);
		$item['itemId'] = $itemid;
		$item['title'] = $prodectName;
		print_r($prodectName);exit;
		$pics = array();
		
		$imgurls = $xml->xpath('//*[@id="slide"]/ul');
		print_r($imgurls);exit;
		foreach ($imgurls[0]->li as $imgurl ) 
		{
			if (count($pics) < 4) 
			{
				if (count($pics) == 0) 
				{
					$iurl = $imgurl->img->attributes()->src;
					if (stripos($iurl, '//') == 0) 
					{
						$iurl .= 'http:' . $iurl;
					}
					$pics[] = $iurl;
				}
				else 
				{
					$iurl = $imgurl->img->attributes()->imgsrc;
					if (stripos($iurl, '//') == 0) 
					{
						$iurl .= 'http:' . $iurl;
					}
					$pics[] = $iurl;
				}
			}
		}
		$item['pics'] = $pics;
		$specs = array();
		$item['total'] = 10;
		$item['sales'] = 0;
		$prodectPriceContent = $xml->xpath('//*[@id="jdPrice"]');
		$prodectPrices = strval( $prodectPriceContent[0]->attributes()->value);
		$item['marketprice'] = $prodectPrices;
		$url =get_jingdong_detail_url($itemid);
		$responseDetail = cfc('http')->ihttp_get($url);
		$contenteDetail = $responseDetail['content'];
		$details = json_decode($contenteDetail, true);
		$prodectContent = $details['wdis'];
		$prodectContent = strval($prodectContent);
		$prodectContent = $this->contentpasswh($prodectContent);
		$item['content'] = $prodectContent;
		$params = array();
		$pr = $details['ware']['wi']['code'];
		$pr = json_decode($pr, 1);
		foreach ($pr as $value ) 
		{
			foreach ($value as $key => $val ) 
			{
				if (is_array($val)) 
				{
					$paramsValue = '';
					foreach ($val as $v ) 
					{
						foreach ($v as $k1 => $v1 ) 
						{
							if (!(empty($v1))) 
							{
								$params[] = array('title' => $k1, 'value' => $v1);
							}
						}
					}
				}
				else if (!(empty($val))) 
				{
					$params[] = array('title' => $key, 'value' => $val);
				}
			}
		}
		$item['params'] = $params;
		return $this->save_jingdong_goods($item, $jingdongurl);
	}
	
	
	
	
function get_tmall_page_url($itemid) {
		$url = 'https://detail.tmall.com/item.htm?id=' . $itemid;
		$url =getRealURL($url);
		return $url;
}

function getRealURL($url) {
		if (function_exists('stream_context_set_default')) 
		{
			stream_context_set_default(array( 'http' => array('method' => 'HEAD') ));
		}
		$header = get_headers($url, 1);
		if (strpos($header[0], '301') || strpos($header[0], '302')) 
		{
			if (is_array($header['Location'])) 
			{
				return $header['Location'][count($header['Location']) - 1];
			}
			return $header['Location'];
		}
		return $url;
}
function my_sort($arrays, $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC) {
		if (is_array($arrays)) 
		{
			foreach ($arrays as $array ) 
			{
				if (is_array($array)) 
				{
					$key_arrays[] = $array[$sort_key];
				}
				else 
				{
					return false;
				}
			}
		}
		else 
		{
			return false;
		}
		array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
		return $arrays;
}
function get_taobao_detail_url($itemid) {
		$url = 'http://hws.m.taobao.com/cache/wdesc/5.0/?id=' . $itemid;
		$url = getRealURL($url);
		return $url;
}
function contentpasswh($content) {
		$content = preg_replace('/(?:width)=(\'|").*?\\1/', ' width="100%"', $content);
		$content = preg_replace('/(?:height)=(\'|").*?\\1/', ' ', $content);
		$content = preg_replace('/(?:max-width:\\s*\\d*\\.?\\d*(px|rem|em))/', '', $content);
		$content = preg_replace('/(?:max-height:\\s*\\d*\\.?\\d*(px|rem|em))/', '', $content);
		$content = preg_replace('/(?:min-width:\\s*\\d*\\.?\\d*(px|rem|em))/', ' ', $content);
		$content = preg_replace('/(?:min-height:\\s*\\d*\\.?\\d*(px|rem|em))/', ' ', $content);
		return $content;
}
function save_taobao_goods($item = array(), $catch_url = '') 
	{   
		$data = array('uniacid' => $_SESSION['uniacid'], 'subtitle' => $item['subTitle'], 'merchid' => $item['merchid'], 'checked' => $item['checked'], 'catch_source' => 'taobao', 'catch_id' => $item['itemId'], 'catch_url' => $catch_url, 'title' => $item['title'], 'total' => $item['total'], 'marketprice' => $item['marketprice'], 'productprice' => $item['productPrice'], 'pcate' => $item['pcate'], 'ccate' => $item['ccate'], 'tcate' => $item['tcate'], 'cates' => $item['cates'], 'sales' => $item['sales'], 'createtime' => time(), 'updatetime' => time(), 'hasoption' => (0 < count($item['options']) ? 1 : 0), 'status' => 0, 'deleted' => 0, 'buylevels' => '', 'showlevels' => '', 'buygroups' => '', 'showgroups' => '', 'noticeopenid' => '', 'storeids' => '', 'merchsale' => ($item['merchid'] == 0 ? 0 : 1), 'newgoods' => 1);
		if (empty($item['merchid'])) 
		{
			$data['discounts'] = '{"type":"0","default":"","default_pay":""}';
		}
		$thumb_url = array();
		$pics = $item['pics'];
		$piclen = count($pics);
		if (0 < $piclen) 
		{
			$img = $this->save_image($pics[0], false);
			if (empty($img)) 
			{
				$img = $pics[0];
			}
			$info = getimagesize('../attachment/' . $img);
			$srcFileExtImg = $info['mime'];
			if ($srcFileExtImg == 'image/x-ms-bmp') 
			{
				$mig = $this->changeBMPtoJPG('../attachment/' . $img);
			}
			else 
			{
				$mig = $img;
			}
			$data['thumb'] = $mig;
			if (1 < $piclen) 
			{
				$i = 1;
				while ($i < $piclen) 
				{
					$img = $this->save_image($pics[$i], false);
					if (empty($img)) 
					{
						$img = $pics[$i];
					}
					$thumb_url[] = $img;
					++$i;
				}
			}
		}
		$mi = array();
		foreach ($thumb_url as $thumb_Info ) 
		{
			$info = getimagesize('../attachment/' . $thumb_Info);
			$srcFileExt = $info['mime'];
			if ($srcFileExt == 'image/x-ms-bmp') 
			{
				$mi[] = $this->changeBMPtoJPG('../attachment/' . $thumb_Info);
			}
			else 
			{
				$mi[] = $thumb_Info;
			}
		}
		$data['thumb_url'] = serialize($mi);
		$goods = pdo_fetch('select * from ' . tablename('jy_weishop_goods') . ' where  catch_id=:catch_id and catch_source=\'taobao\' and uniacid=:uniacid and merchid=:merchid', array(':catch_id' => $item['itemId'], ':uniacid' => $_SESSION['uniacid'], ':merchid' => $item['merchid']));
		if (empty($goods)) 
		{
			pdo_insert('jy_weishop_goods', $data);
			$goodsid = pdo_insertid();
		}
		else 
		{
			$goodsid = $goods['id'];
			unset($data['createtime']);
			pdo_update('jy_weishop_goods', $data, array('id' => $goodsid));
		}
		$goods_params = pdo_fetchall('select * from ' . tablename('jy_weishop_goods_param') . ' where goodsid=:goodsid ', array(':goodsid' => $goodsid));
		$params = $item['params'];
		$paramids = array();
		$displayorder = 0;
		foreach ($params as $p ) 
		{
			$oldp = pdo_fetch('select * from ' . tablename('jy_weishop_goods_param') . ' where goodsid=:goodsid and title=:title limit 1', array(':goodsid' => $goodsid, ':title' => $p['title']));
			$paramid = 0;
			$d = array('uniacid' => $_SESSION['uniacid'], 'goodsid' => $goodsid, 'title' => $p['title'], 'value' => $p['value'], 'displayorder' => $displayorder);
			if (empty($oldp)) 
			{
				pdo_insert('jy_weishop_goods_param', $d);
				$paramid = pdo_insertid();
			}
			else 
			{
				pdo_update('jy_weishop_goods_param', $d, array('id' => $oldp['id']));
				$paramid = $oldp['id'];
			}
			$paramids[] = $paramid;
			++$displayorder;
		}
		if (0 < count($paramids)) 
		{
			pdo_query('delete from ' . tablename('jy_weishop_goods_param') . ' where goodsid=:goodsid and id not in (' . implode(',', $paramids) . ')', array(':goodsid' => $goodsid));
		}
		else 
		{
			pdo_query('delete from ' . tablename('jy_weishop_goods_param') . ' where goodsid=:goodsid ', array(':goodsid' => $goodsid));
		}
		$specs = $item['specs'];
		$specids = array();
		$displayorder = 0;
		$newspecs = array();
		foreach ($specs as $spec ) 
		{
			$oldspec = pdo_fetch('select * from ' . tablename('jy_weishop_goods_spec') . ' where goodsid=:goodsid and propId=:propId limit 1', array(':goodsid' => $goodsid, ':propId' => $spec['propId']));
			$specid = 0;
			$d_spec = array('uniacid' => $_SESSION['uniacid'], 'goodsid' => $goodsid, 'title' => $spec['title'], 'displayorder' => $displayorder, 'propId' => $spec['propId']);
			if (empty($oldspec)) 
			{
				pdo_insert('jy_weishop_goods_spec', $d_spec);
				$specid = pdo_insertid();
			}
			else 
			{
				pdo_update('jy_weishop_goods_spec', $d_spec, array('id' => $oldspec['id']));
				$specid = $oldspec['id'];
			}
			$d_spec['id'] = $specid;
			$specids[] = $specid;
			++$displayorder;
			$spec_items = $spec['items'];
			$spec_itemids = array();
			$displayorder_item = 0;
			$newspecitems = array();
			foreach ($spec_items as $spec_item ) 
			{
				$d = array('uniacid' => $_SESSION['uniacid'], 'specid' => $specid, 'title' => $spec_item['title'], 'thumb' => $this->save_image($spec_item['thumb'], false), 'valueId' => $spec_item['valueId'], 'show' => 1, 'displayorder' => $displayorder_item);
				$oldspecitem = pdo_fetch('select * from ' . tablename('jy_weishop_goods_spec_item') . ' where specid=:specid and valueId=:valueId limit 1', array(':specid' => $specid, ':valueId' => $spec_item['valueId']));
				$spec_item_id = 0;
				if (empty($oldspecitem)) 
				{
					pdo_insert('jy_weishop_goods_spec_item', $d);
					$spec_item_id = pdo_insertid();
				}
				else 
				{
					pdo_update('jy_weishop_goods_spec_item', $d, array('id' => $oldspecitem['id']));
					$spec_item_id = $oldspecitem['id'];
				}
				++$displayorder_item;
				$spec_itemids[] = $spec_item_id;
				$d['id'] = $spec_item_id;
				$newspecitems[] = $d;
			}
			$d_spec['items'] = $newspecitems;
			$newspecs[] = $d_spec;
			if (0 < count($spec_itemids)) 
			{
				pdo_query('delete from ' . tablename('jy_weishop_goods_spec_item') . ' where specid=:specid and id not in (' . implode(',', $spec_itemids) . ')', array(':specid' => $specid));
			}
			else 
			{
				pdo_query('delete from ' . tablename('jy_weishop_goods_spec_item') . ' where specid=:specid ', array(':specid' => $specid));
			}
			pdo_update('jy_weishop_goods_spec', array('content' => serialize($spec_itemids)), array('id' => $oldspec['id']));
		}
		if (0 < count($specids)) 
		{
			pdo_query('delete from ' . tablename('jy_weishop_goods_spec') . ' where goodsid=:goodsid and id not in (' . implode(',', $specids) . ')', array(':goodsid' => $goodsid));
		}
		else 
		{
			pdo_query('delete from ' . tablename('jy_weishop_goods_spec') . ' where goodsid=:goodsid ', array(':goodsid' => $goodsid));
		}
		$minprice = 0;
		$options = $item['options'];
		if (0 < count($options)) 
		{
			$minprice = $options[0]['marketprice'];
		}
		$optionids = array();
		$displayorder = 0;
		foreach ($options as $o ) 
		{
			$option_specs = $o['option_specs'];
			$ids = array();
			$valueIds = array();
			foreach ($option_specs as $os ) 
			{
				foreach ($newspecs as $nsp ) 
				{
					foreach ($nsp['items'] as $nspitem ) 
					{
						if ($nspitem['valueId'] == $os['valueId']) 
						{
							$ids[] = $nspitem['id'];
							$valueIds[] = $nspitem['valueId'];
						}
					}
				}
			}
			asort($ids);
			$ids = implode('_', $ids);
			$valueIds = implode('_', $valueIds);
			$do = array('uniacid' => $_SESSION['uniacid'], 'displayorder' => $displayorder, 'goodsid' => $goodsid, 'title' => implode('+', $o['title']), 'specs' => $ids, 'stock' => $o['stock'], 'marketprice' => $o['marketprice'], 'skuId' => $o['skuId']);
			if ($o['marketprice'] < $minprice) 
			{
				$minprice = $o['marketprice'];
			}
			$oldoption = pdo_fetch('select * from ' . tablename('jy_weishop_goods_option') . ' where goodsid=:goodsid and skuId=:skuId limit 1', array(':goodsid' => $goodsid, ':skuId' => $o['skuId']));
			$option_id = 0;
			if (empty($oldoption)) 
			{
				pdo_insert('jy_weishop_goods_option', $do);
				$option_id = pdo_insertid();
			}
			else 
			{
				pdo_update('jy_weishop_goods_option', $do, array('id' => $oldoption['id']));
				$option_id = $oldoption['id'];
			}
			++$displayorder;
			$optionids[] = $option_id;
		}
		if (0 < count($optionids)) 
		{
			pdo_query('delete from ' . tablename('jy_weishop_goods_option') . ' where goodsid=:goodsid and id not in (' . implode(',', $optionids) . ')', array(':goodsid' => $goodsid));
		}
		else 
		{
			pdo_query('delete from ' . tablename('jy_weishop_goods_option') . ' where goodsid=:goodsid ', array(':goodsid' => $goodsid));
		}
		$response = $item['content'];
		$content = $response['content'];
		preg_match_all('/<img.*?src=[\\\\\'| \\"](.*?(?:[\\.gif|\\.jpg]?))[\\\\\'|\\"].*?[\\/]?>/', $content, $imgs);
		if (isset($imgs[1])) 
		{
			foreach ($imgs[1] as $img ) 
			{
				$catchimg = $img;
				if (substr($catchimg, 0, 2) == '//') 
				{
					$img = 'http://' . substr($img, 2);
				}
				$im = array('catchimg' => $catchimg, 'system' => $this->save_image($img, true));
				$images[] = $im;
			}
		}
		preg_match('/tfsContent : \\\'(.*)\\\'/', $content, $html);
		$html = iconv('GBK', 'UTF-8', $html[1]);
		if (isset($images)) 
		{
			foreach ($images as $img ) 
			{
				if (!(empty($img['system']))) 
				{
					$html = str_replace($img['catchimg'], $img['system'], $html);
				}
			}
		}
		$html = m('common')->html_to_images($html);
		$hasoption = 0;
		if (0 < count($options)) 
		{
			$hasoption = 1;
		}
		$d = array('content' => $html, 'hasoption' => $hasoption);
		if (0 < $minprice) 
		{
			$d['marketprice'] = $minprice;
		}
		pdo_update('jy_weishop_goods', $d, array('id' => $goodsid));
		if ($d['hasoption']) 
		{
			$sql = 'update ' . tablename('jy_weishop_goods') . ' g set' . "\r\n" . '            g.minprice = (select min(marketprice) from ' . tablename('jy_weishop_goods_option') . ' where goodsid = ' . $goodsid . ' and marketprice > 0),' . "\r\n" . '            g.maxprice = (select max(marketprice) from ' . tablename('jy_weishop_goods_option') . ' where goodsid = ' . $goodsid . ')' . "\r\n" . '            where g.id = ' . $goodsid . ' and g.hasoption=1';
		}
		else 
		{
			$sql = 'update ' . tablename('jy_weishop_goods') . ' set minprice = marketprice,maxprice = marketprice where id = ' . $goodsid . ' and hasoption=0;';
		}
		pdo_query($sql);
		return array('result' => '1', 'goodsid' => $goodsid);
	}
function get_jingdong_info_url($itemid) {
	$url = 'http://item.m.jd.com/ware/view.action?wareId=' . $itemid;
	$url =getRealURL($url);
	return $url;
}

function get_item_jdHK($itemid = '', $jingdongurl = '', $cates = '', $merchid = 0){
		error_reporting(0);
		$g = pdo_fetch('select * from ' . tablename('jy_weishop_goods') . ' where uniacid=:uniacid and merchid=:merchid and catch_id=:catch_id and catch_source=\'jingdong\' limit 1', array(':uniacid' => $_SESSION['uniacid'], ':catch_id' => $itemid, ':merchid' => $merchid));
		$url = 'http://item.jd.hk/' . $itemid . '.html';
		load()->func('communication');
		$response = cfc('http')->ihttp_get($url);
		$length = strval($response['headers']['Content-Length']);
		if (empty($length)) 
		{
			return array('result' => '0', 'error' => '未从京东获取到商品信息!');
		}
		$content = iconv('GBK', 'UTF-8', $response['content']);
		preg_match('/<div class="sku-name">\\n{1}[\\s\\S\\n]*<\\/span>\\n(.+)<\\/div>\\n\\s*<div class="news">/', $content, $prodectName);
		$prodectName = trim($prodectName[1]);
		if ($prodectName == NULL) 
		{
			return array('result' => '0', 'error' => '宝贝不存在!');
		}
		$item = array();
		$item['id'] = $g['id'];
		$item['merchid'] = $merchid;
		if (!(empty($merchid))) 
		{
			if (empty($_W['merch_user']['goodschecked'])) 
			{
				$item['checked'] = 1;
			}
			else 
			{
				$item['checked'] = 0;
			}
		}
		$pcates = array();
		$ccates = array();
		$tcates = array();
		$pcateid = 0;
		$ccateid = 0;
		$tcateid = 0;
		if (is_array($cates)) 
		{
			foreach ($cates as $key => $cid ) 
			{
				$c = pdo_fetch('select level from ' . tablename('jy_weishop_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $cid, ':uniacid' => $_SESSION['uniacid']));
				if ($c['level'] == 1) 
				{
					$pcates[] = $cid;
				}
				else if ($c['level'] == 2) 
				{
					$ccates[] = $cid;
				}
				else if ($c['level'] == 3) 
				{
					$tcates[] = $cid;
				}
				if ($key == 0) 
				{
					if ($c['level'] == 1) 
					{
						$pcateid = $cid;
					}
					else if ($c['level'] == 2) 
					{
						$crow = pdo_fetch('select parentid from ' . tablename('jy_weishop_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $cid, ':uniacid' => $_SESSION['uniacid']));
						$pcateid = $crow['parentid'];
						$ccateid = $cid;
					}
					else if ($c['level'] == 3) 
					{
						$tcateid = $cid;
						$tcate = pdo_fetch('select id,parentid from ' . tablename('jy_weishop_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $cid, ':uniacid' => $_SESSION['uniacid']));
						$ccateid = $tcate['parentid'];
						$ccate = pdo_fetch('select id,parentid from ' . tablename('jy_weishop_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $ccateid, ':uniacid' => $_SESSION['uniacid']));
						$pcateid = $ccate['parentid'];
					}
				}
			}
		}
		$item['pcate'] = $pcateid;
		$item['ccate'] = $ccateid;
		$item['tcate'] = $tcateid;
		if (!(empty($cates))) 
		{
			$item['cates'] = implode(',', $cates);
		}
		$item['pcates'] = implode(',', $pcates);
		$item['ccates'] = implode(',', $ccates);
		$item['tcates'] = implode(',', $tcates);
		$item['itemId'] = $itemid;
		$item['title'] = $prodectName;
		$pics = array();
		preg_match_all('/<img.+src=\'(.+)\' data-url.+data-img=\'1\' width=\'75\' height=\'75\'>/', $content, $picRet);
		if (empty($picRet[1])) 
		{
			return array('result' => '0', 'error' => '不能抓取到图片');
		}
		foreach ($picRet[1] as $pic ) 
		{
			$pics[] = 'https:' . str_replace('s75x75', 's450x450', $pic);
		}
		$item['pics'] = $pics;
		$specs = array();
		$item['total'] = 10;
		$item['sales'] = 0;
		$priceContent = ihttp_get('https://p.3.cn/prices/mgets?skuIds=J_' . $itemid);
		$prodectPrices = json_decode($priceContent['content'], 1);
		$item['marketprice'] = $prodectPrices[0]['p'];
		$url =get_jingdong_detail_url($itemid);
		$responseDetail = ihttp_get($url);
		$contenteDetail = $responseDetail['content'];
		$details = json_decode($contenteDetail, true);
		$prodectContent = $details['wdis'];
		$prodectContent = strval($prodectContent);
		$prodectContent = $this->contentpasswh($prodectContent);
		$item['content'] = $prodectContent;
		$params = array();
		$pr = $details['ware']['wi']['code'];
		preg_match_all('/<td class="tdTitle">(.*?)<\\/td>/i', $pr, $params1);
		preg_match_all('/<td>(.*?)<\\/td>/i', $pr, $params2);
		$paramsTitle = $params1[1];
		$paramsValue = $params2[1];
		if (count($paramsTitle) == count($paramsValue)) 
		{
			$i = 0;
			while ($i < count($paramsTitle)) 
			{
				$params[] = array('title' => $paramsTitle[$i], 'value' => $paramsValue[$i]);
				++$i;
			}
		}
		$item['params'] = $params;
		return $this->save_jingdong_goods($item, $jingdongurl);
	}
function get_jingdong_detail_url($itemid){
		$url = 'http://item.m.jd.com/ware/detail.json?wareId=' . $itemid;
		$url =getRealURL($url);
		return $url;
}	